package com.ruoyi.web.controller.pay;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.pay.entity.PayTradeOrder;
import com.ruoyi.pay.service.PayTradeOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支付订单Controller
 *
 * @author ruoyi
 * @date 2020-10-10
 */
@Controller
@RequestMapping("/system/tradeorder")
public class PayTradeOrderController extends BaseController
{
    private String prefix = "system/tradeorder";

    @Autowired
    private PayTradeOrderService payTradeOrderService;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/tradeorder";
    }

    /**
     * 查询支付订单列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PayTradeOrder payTradeOrder)
    {
        startPage();
        List<PayTradeOrder> list = payTradeOrderService.list(Wrappers.query(payTradeOrder));
        return getDataTable(list);
    }

    /**
     * 导出支付订单列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "支付订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PayTradeOrder payTradeOrder)
    {
        List<PayTradeOrder> list = payTradeOrderService.list(Wrappers.query(payTradeOrder));
        ExcelUtil<PayTradeOrder> util = new ExcelUtil<PayTradeOrder>(PayTradeOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增支付订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存支付订单
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PayTradeOrder payTradeOrder)
    {
        return toAjax(payTradeOrderService.save(payTradeOrder));
    }

    /**
     * 修改支付订单
     */
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") String orderId, ModelMap mmap)
    {
        PayTradeOrder payTradeOrder = payTradeOrderService.getById(orderId);
        mmap.put("payTradeOrder", payTradeOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存支付订单
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PayTradeOrder payTradeOrder)
    {
        return toAjax(payTradeOrderService.updateById(payTradeOrder));
    }

    /**
     * 删除支付订单
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(payTradeOrderService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
